﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Cloud.Dialogflow.V2;
using Bot.Domain.DialogFlow;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Bot.Domain
{
    public class DetectIntentContentDto
    {
        public QueryInput queryInput { get; set; }
        //public string audioConfig { get; set; }
    }
    public class QueryInput
    {
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TextContent text { get; set; }
        [JsonProperty(PropertyName = "event", NullValueHandling = NullValueHandling.Ignore)]
        public EventContent eventContent { get; set; }
    }
    public class TextContent
    {
        public string text { get; set; }
        public string languageCode { get; set; }
    }
    public class EventContent
    {
        public string name { get; set; }
        public string languageCode { get; set; }
    }
    public class DetectIntentResponseDto
    {
        public JObject intent { get; set; }
        public string intentId { get; set; }
        public string intentName { get; set; }
        public decimal intentDetectionConfidence { get; set; }
        public Dictionary<string, string> Parameters { get; set; }
        public string responseId { get; set; }
        public bool allRequiredParamsPresent { get; set; }
        public string fulfillmentText { get; set; }
        /// <summary>
        /// 所有的intent匹配分数2019.6.19 
        /// </summary>
        public dynamic allDetectIntents { get; set; }
        /// <summary>
        /// 2019.9.4 own bot 本次需要收集的追问 id
        /// </summary>
        public Guid collectionPromotEntityId { get; set; }
    }
    public static class IntentContentConvertor
    {
        public static DetectIntentContentDto FromDialogflow(DetectIntentRequest request)
        {
            DetectIntentContentDto content = new DetectIntentContentDto();
            if (request.QueryInput != null)
            {
                QueryInput quertInput = new QueryInput();
                if (request.QueryInput.Text != null)
                {
                    TextContent txt = new TextContent();
                    txt.text = request.QueryInput.Text.Text;
                    txt.languageCode = request.QueryInput.Text.LanguageCode;
                    quertInput.text = txt;
                    quertInput.eventContent = null;
                }
                else if (request.QueryInput.Event != null)
                {
                    EventContent eve = new EventContent();
                    eve.name = request.QueryInput.Event.Name;
                    eve.languageCode = request.QueryInput.Event.LanguageCode;
                    quertInput.eventContent = eve;
                    quertInput.text = null;
                }
                content.queryInput = quertInput;
            }
            return content;
        }
    }
    /// <summary>
    /// extention utils
    /// </summary>
    public static class extendUtils
    {
        /// <summary>
        /// get value from jobject by key
        /// </summary>
        /// <param name="container"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetStringValue(this JObject container, string key)
        {
            JToken value = null;
            if (container.TryGetValue(key, out value))
            {
                return value.ToString();
            }
            return string.Empty;
        }
    }
    public class DetectIntentResponseForDialogFlow
    {
        public QueryResultForDialogFlow queryResult { get; set; }
        public string responseId { get; set; }
    }
    public class QueryResultForDialogFlow
    {
        public string action { get; set; }
        public JObject parameters { get; set; }
        public bool allRequiredParamsPresent { get; set; }
        public List<Context> outputContexts { get; set; }
        public JObject intent { get; set; }
        public string queryText { get; set; }
        public float intentDetectionConfidence { get; set; }
        public string languageCode { get; set; }
        public string fulfillmentText { get; set; }
    }
}
